<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * manage Article
 *
 * @package     manage
 * @subpackage  
 * @category    Article -  文章
 * @author      ChenFu
 * @since       2015-6-17 version 1.0
 * -----------------------------------------------------    
 */
class Article extends CI_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	  	$this->load->library('form_validation'); // 表单验证类
		$this->load->model('category_model');    // 加载分类模型
		$this->load->model('article_model');     // 加载文章模型 
		check_login(); // 检查是否登录
	}

	/**
	 * 
	 * @return [type] [list]
	 */
	public function index()
	{
		$total = $this->article_model->get_num_rows();
		if($total != 0){
			// 加载分页类
			$arr = array('page_total_num' => $total,'every_page_num'=>10,'page_url_name'=>'page_id');
			$this->load->library('page',$arr);
			$data['pager']	  = $this->page->pages_all();
			$data['arr'] 	  = $this->article_model->get_all_data_and_page($this->page->limit);
		}else{
			$data['error'] = '暂无文章!';
		}
		$this->load->view('admin/article/index',$data);
	}

	/**
	 * 增加文章
	 */
	public function insert()
	{
		if(!empty($_POST['art_title'])){
				$arr = $this->input->post();
				$_insert['art_pid'] 		= $arr['art_pid'];
				$_insert['art_title']	    = $arr['art_title'];
				$_insert['art_author']		= $arr['art_author'];
				$_insert['art_source']		= $arr['art_source'];
				$_insert['art_sort']		= $arr['art_sort'];
				$_insert['art_status']		= $arr['art_status'];	
				$_insert['art_content']		= $arr['art_content'];
				$_insert['art_reading']		= 1;
				$_insert['art_date']		= time();
				if(!empty($_FILES['imgurl']['name'])){ 	// 判断是否上传缩略图
					$_insert['art_cover_url'] = self::itonimg('imgurl','artimg');
				}
				if($this->article_model->insert($_insert)){
					$this->sys_message->skip('增加文章成功',site_url('manage/article'));
					return ;
				}else{
					$this->sys_message->show_error('增加文章失败,请重新添加');
					return ;
				}
		}

		$data['category'] = $this->category_model->to_options(0,0); // 栏目数据
		$this->load->view('admin/article/insert',$data);
	}

	/**
	 * 上传图片处理
	 */
	public function itonimg($name,$file_name){
		$config['upload_path']   ='./publics/uploads/'.$file_name; // 上传路径
		$config['allowed_types'] = 'gif|jpg|png';				   // 允许图片格式
		$config['max_size'] 	 = '100000';					   // 允许大小
		$config['overwrite']	 = FALSE;						   // 是否重名	
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload',$config); // 加载上传类
		if($this->upload->do_upload($name)){
		  $data = array('upload_data' => $this->upload->data());
		  return  './publics/uploads/'.$file_name.'/'.$data['upload_data']['file_name'];
		}
	}

	/**
	 * 编辑文章信息
	 */
	public function edit()
	{
		// 默认值
		$data['artarr'] = $this->article_model->get_art_by_aid($this->input->get('id'));
		// 角色
		$data['category'] = $this->category_model->to_options(0,0,$data['artarr']['art_id']); // 栏目数据
		if ($this->form_validation->run('article') == FALSE){
		    $this->load->view('admin/article/edit',$data);
		}else{
			if(!empty($_POST['art_title'])){
				$arr = $this->input->post();
				$_update['art_pid'] 		= $arr['art_pid'];
				$_update['art_title']	    = $arr['art_title'];
				$_update['art_author']		= $arr['art_author'];
				$_update['art_source']		= $arr['art_source'];
				$_update['art_sort']		= $arr['art_sort'];
				$_update['art_status']		= $arr['art_status'];	
				$_update['art_content']		= $arr['art_content'];
				if(!empty($_FILES['imgurl']['name'])){ 	// 判断是否上传缩略图
					$_update['art_cover_url'] = self::itonimg('imgurl','artimg');
				}
				if($this->article_model->update($_update,$_GET['id'])){
					$this->sys_message->skip('编辑成功',base_url('manage/article'));
				}else{
					$this->sys_message->show_error('编辑失败,请重新编辑！');
				}
			}
		}
	}

	/**
	 * 删除文章
	 */
	public function delete()
	{
		if(!empty($_GET['id']) && intval($_GET['id'])){
			if($this->article_model->delete($_GET['id'])){
				die('ok');
			}else{
				die('no');
			}
		}else{
			die('error_1');
		}
	}


}